layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;
    //用户列表
    var tableIns = table.render({
        elem: '#list',
        url : '/admin/sys/role/search.do',
        page : true,
        limits : [10,15,20,25],
        limit : 20,
        id : "listTable",
        cols : [[
            {field: 'roleId', title: '角色id',  align:"center"},
            {field: 'roleName', title: '角色名',  align:'center'},
            {field: 'remark', title: '备注', align:'center'  },
            {field: 'createTime',  title: '创建时间', align:'center', templet:function(d){
                return moment(d.createTime).format("YYYY-MM-DD HH:mm:ss");
            }},
            {title: '操作', templet:'#listBar',fixed:"right",align:"center"}
        ]]
    });

    $(".search_btn").on("click",function(){
            table.reload("listTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    roleId: $('#roleId').val(),
                    roleName: $('#roleName').val()
                }
            })
    });

    function addRole(edit){
        var index = layui.layer.open({
            title : "添加角色",
            type : 2,
            content : edit?"edit.html?roleId="+edit.roleId:"edit.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find("#roleId").val(edit.roleId);
                    body.find("#roleName").val(edit.roleName);
                    body.find("#remark").val(edit.remark);
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回列表页', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
    }
    $("#addNews_btn").click(function(){
        addRole();
    });
    //列表操作
    table.on('tool(list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addRole(data);
        }
    });

});
